package com.ezcoding.controller;

import com.ezcoding.service.ProductService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.Map;

@Before(Tx.class)
public class ProductController extends Controller {

    private Map<String, Object> addOrUpdate(int addOrUpdate) {
        String companyId = getPara("companyId");
        String productId = getPara("productId");

        String type = getPara("type");
        String name = getPara("name");
        String unit = getPara("unit");
        String productNum = getPara("productNum");
        String upperLimit = getPara("upperLimit");
        String lowerLimit = getPara("lowerLimit");

        String brand = getPara("brand");
        String originPlace = getPara("originPlace");
        String specifications = getPara("specifications");
        String barCode = getPara("barCode");
        String remark = getPara("remark");

        String guidePrice1 = getPara("guidePrice1");
        String guidePrice2 = getPara("guidePrice2");

        String primaryDirectory = getPara("primaryDirectory");
        String secondaryDirectory = getPara("secondaryDirectory");

        if (addOrUpdate == ProductService.TYPE_ADD) {
            return ProductService.getInstance().addProduct(
                    companyId,
                    type,
                    name,
                    unit,
                    productNum,
                    upperLimit,
                    lowerLimit,
                    brand,
                    originPlace,
                    specifications,
                    barCode,
                    remark,
                    guidePrice1,
                    guidePrice2,
                    primaryDirectory,
                    secondaryDirectory);
        } else {
            return ProductService.getInstance().updateProduct(
                    companyId,
                    productId,
                    type,
                    name,
                    unit,
                    productNum,
                    upperLimit,
                    lowerLimit,
                    brand,
                    originPlace,
                    specifications,
                    barCode,
                    remark,
                    guidePrice1,
                    guidePrice2,
                    primaryDirectory,
                    secondaryDirectory);
        }
    }

    public void addProduct() {
        renderJson(addOrUpdate(ProductService.TYPE_ADD));
    }

    public void getProductsByCompanyId() {
        String companyId = getPara("companyId");

        renderJson(ProductService.getInstance().getProductsByCompanyId(companyId));
    }

    public void updateProduct() {
        renderJson(addOrUpdate(ProductService.TYPE_UPDATE));
    }

    public void uploadProduct() {
        String companyId = getPara("companyId");
        String products = getPara("products");

        renderJson(ProductService.getInstance().uploadProduct(companyId, products));
    }

    public void uploadProductFinished() {
        String username = getPara("username");
        double totalMoney = Double.parseDouble(getPara("totalMoney"));

        renderJson(ProductService.getInstance().uploadProductFinished(username, totalMoney));
    }

    public void addFormula() {
        String companyId = getPara("companyId");
        String combineId = getPara("combineId");
        String combineCount = getPara("combineCount");
        String splitIds = getPara("splitIds");
        String splitCounts = getPara("splitCounts");

        renderJson(ProductService.getInstance().addFormula(companyId, combineId, combineCount, splitIds, splitCounts));
    }

    public void deleteFormula() {
        String companyId = getPara("companyId");
        String combineId = getPara("combineId");

        renderJson(ProductService.getInstance().deleteFormula(companyId, combineId));
    }

    public void getFormulasByCompanyId() {
        String companyId = getPara("companyId");

        renderJson(ProductService.getInstance().getFormulasByCompanyId(companyId));
    }

    public void combineProduct() {
        String companyId = getPara("companyId");
        String combineId = getPara("combineId");
        String combineCount = getPara("combineCount");

        renderJson(ProductService.getInstance().combineProduct(companyId, combineId, combineCount));
    }

    public void splitProduct() {
        String username = getPara("username");
        String companyId = getPara("companyId");
        String combineId = getPara("combineId");
        String splitCount = getPara("splitCount");
        String splitInPrices = getPara("splitInPrices");

        renderJson(ProductService.getInstance().splitProduct(
                username,
                companyId,
                combineId,
                splitCount,
                splitInPrices));
    }

    public void countProduct() {
        String username = getPara("username");
        String companyId = getPara("companyId");
        String products = getPara("products");

        renderJson(ProductService.getInstance().countProduct(username, companyId, products));
    }

    public void sanbaoMove() {
        String companyId = getPara("companyId");
        String products = getPara("products");
        String moveType = getPara("moveType");
        String moveCount = getPara("moveCount");

        renderJson(ProductService.getInstance().sanbaoMove(companyId, products, moveType, moveCount));
    }

    public void updateHistoryProduct() {
        String historyProducts = getPara("historyProducts");
        //type 0:通过我方商品id更新历史价格记录 1:通过对方商品id更新历史价格记录
        int type = getParaToInt("type");

        renderJson(ProductService.getInstance().updateHistoryProducts(historyProducts, type));
    }

    /**
     * 通过商品id获取历史价格记录
     */
    public void getAllHistoryProdycts() {
        String sourceCompanyId = getPara("sourceCompanyId");
        String targetCompanyId = getPara("targetCompanyId");
        String productIds = getPara("productIds");
        //type 0：销售 1:采购
        int type = getParaToInt("type");

        renderJson(ProductService.getInstance().getAllHistoryProducts(
                sourceCompanyId,
                targetCompanyId,
                productIds,
                type));
    }

    /**
     * 删除商品
     */
    public void deleteProduct() {
        String companyId = getPara("companyId");
        String productId = getPara("productId");

        renderJson(ProductService.getInstance().deleteProduct(companyId, productId));
    }

    /**
     * 根据客户端发送的商品id刷新客户端产品信息
     */
    public void refreshProducts() {
        String companyId = getPara("companyId");
        String productIds = getPara("productIds");

        renderJson(ProductService.getInstance().refreshProducts(companyId, productIds));
    }

    /**
     * 通过企业Id总成商品Id获取对应装备拆分公式信息
     */
    public void getFormulaInfo() {
        String companyId = getPara("companyId");
        String combineId = getPara("combineId");

        renderJson(ProductService.getInstance().getFormulaInfo(companyId, combineId));
    }
}
